/*									tab:4
 * "Copyright (c) 2005 The Regents of the University  of California.  
 * All rights reserved.
 *
 * Permission to use, copy, modify, and distribute this software and
 * its documentation for any purpose, without fee, and without written
 * agreement is hereby granted, provided that the above copyright
 * notice, the following two paragraphs and the author appear in all
 * copies of this software.
 * 
 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY
 * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
 * DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
 * DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
 *
 */

/**
 * Java-side application for testing serial port communication.
 * 
 *
 * @author Phil Levis <pal@cs.berkeley.edu>
 * @date August 12 2005
 */

import java.io.DataOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

import net.tinyos.message.*;
import net.tinyos.packet.*;
import net.tinyos.util.*;

public class TestSensor implements MessageListener {

  private MoteIF moteIF;
  
  public TestSensor(MoteIF moteIF) {
    this.moteIF = moteIF;
    this.moteIF.registerListener(new TestSensorMsg(), this);
  }


  public void messageReceived(int to, Message message) {
    TestSensorMsg msg = (TestSensorMsg)message;
    //System.out.println("Type: " + msg.get_Type());
    //System.out.println("Data: " + msg.get_Data());
    SendtoServer("http://192.168.205.1:8080/Test/ServletDemo1",message);  //将数据发送到指定服务器
  }
  

  public static void SendtoServer(String url,Message message){				//发送函数
		TestSensorMsg msg = (TestSensorMsg)message;
		//String output = "type="+msg.get_Type()+"&data="+msg.get_Data();
		//JSON格式
		String output = "[{"+'"'+"Type"+'"'+":"+msg.get_Type()+","+'"'+"Data"+'"'+":"+msg.get_Data()+"}]";
		try{
			
			URL Url = new URL(url);
			HttpURLConnection  conn = null;
			conn = (HttpURLConnection)Url.openConnection();
			conn.setRequestMethod("POST");
			conn.setConnectTimeout(8000);
			conn.setDoOutput(true);
			conn.setReadTimeout(8000);
			DataOutputStream out = new DataOutputStream(conn.getOutputStream());
			out.writeBytes(output);
			out.close();
			
			System.out.println(conn.getResponseCode());
			
	}catch(Exception e){
		e.printStackTrace();
	}finally{
		
	}
  }
 
  
  private static void usage() {
    System.err.println("usage: TestSensor [-comm <source>]");
  }
  
  public static void main(String[] args) throws Exception {
    String source = null;
    if (args.length == 2) {
      if (!args[0].equals("-comm")) {
	usage();
	System.exit(1);
      }
      source = args[1];
    }
    else if (args.length != 0) {
      usage();
      System.exit(1);
    }
    

    PhoenixSource phoenix;

    
    if (source == null) {
      phoenix = BuildSource.makePhoenix(PrintStreamMessenger.err);
    }
    else {
      phoenix = BuildSource.makePhoenix(source, PrintStreamMessenger.err);
    }

    MoteIF mif = new MoteIF(phoenix);
    TestSensor serial = new TestSensor(mif);
    //serial.sendPackets();
  }


}
